package com.novak.rss.reader;

import java.util.ArrayList;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
/**
 * 
 * @author novak 
 * hihitest
 *
 */

public class AddViewActivity extends Activity
{
  private Button button;
  private EditText url;
  private ArrayList<String> urls = null;

  @Override
  protected void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add);
    urls = getIntent().getExtras().getStringArrayList("urls");
    url = (EditText) findViewById(R.id.url_to_add);
    button = (Button) findViewById(R.id.to_add);
    button.setOnClickListener(new Button.OnClickListener()
    {
      @Override
      public void onClick(View v)
      {
        String urlToAdd = url.getText().toString().trim();
        if (urlToAdd.equals("http://") || urlToAdd.equals(""))
        {
          showError(getResources().getString(R.string.null_input));
        } else if (urls.contains(urlToAdd))
        {
          showError(getResources().getString(R.string.repeated));
        } else
        {
          try
          {
            RSSHelper helper = new RSSHelper(urlToAdd);
            Intent intent = new Intent();
            Bundle b = new Bundle();
            b.putString("name", helper.getCi().getTitle());
            b.putString("url", urlToAdd);
            intent.putExtras(b);
            setResult(1, intent);
            finish();
          } catch (Exception e)
          {
            showError(e.toString());
          }

        }
      }
    });

  }

  private void showError(String mess)
  {
    new AlertDialog.Builder(AddViewActivity.this).setTitle("Message")
        .setMessage(mess).setNegativeButton(
            getResources().getString(R.string.ok),
            new DialogInterface.OnClickListener()
            {
              public void onClick(DialogInterface dialog, int which)
              {
              }
            }).show();
  }

}
